<template>
<div>
    <van-picker title="更改性别" :default-index="gender" show-toolbar :columns="columns" @cancel="onCancel" @confirm="onConfirm" />
</div>
</template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: '',
  props: {
    gender: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      columns: ['男', '女']
    }
  },
  methods: {
    onCancel () {
      this.$emit('close')
    },
    async onConfirm (value, index) {
      this.$emit('change', index)
      this.$emit('close')
      this.$toast.loading({
        message: '更改中...',
        forbidClick: true
      })
      try {
        await updateUserProfile({
          gender: index
        })
        this.$toast.success('更改性别成功')
      } catch (err) {
        this.$toast.fail('更改性别失败')
      }
    }
  }
}
</script>

<style scoped lang="less">

</style>
